import math

# import torch
# from torch.nn.parameter import Parameter
# from torch.nn.modules.module import Module
# from torch.nn import init

import mindspore
from mindspore import nn, ops, Parameter
from mindspore.common import initializer
from mindspore.common.initializer import XavierUniform, Constant, Uniform

class GraphConvolution(nn.Cell):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
    """

    def __init__(self, in_features, out_features, bias=True):
        super(GraphConvolution, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(mindspore.Tensor(shape=(in_features,out_features),dtype=mindspore.float32,init=XavierUniform()))

        self.weight = initializer(XavierUniform(), self.weight, mindspore.float32)
        if bias:
            self.bias = Parameter(mindspore.Tensor(shape=(out_features),dtype=mindspore.float32,init=XavierUniform()))
            constant_init = Constant(value=0)
            constant_init(self.bias)
        else:
            self.register_parameter('bias', None)

        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data = initializer(Uniform(stdv), self.weight.data, mindspore.float32)
        if self.bias is not None:
            self.bias.data = initializer(Uniform(stdv), self.bias.data, mindspore.float32)

    def construct(self, input, adj):
        support = ops.MatMul(input, self.weight)
        output = ops.MatMul(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'
